package com.websocet.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.websocet.domain.MessageVo;
import com.websocet.domain.TestJson;
import com.websocet.service.TestJsonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @Author: 陈玉鹏
 * @Description: TODO
 * @DateTime: 2023/6/1 22:05
 */
@RestController
@RequestMapping("/testjson")
public class TestJsonController {

    @Autowired
    private TestJsonService testJsonService;

    @GetMapping("/save")
    public boolean save () {
        List<TestJson> list = new ArrayList<>();
        for (int i=1;i<3;i++) {
            TestJson testJson = new TestJson();
            testJson.setDeptId(Long.valueOf(i));
            List<MessageVo> list1 = new ArrayList<>();
            MessageVo messageVo = new MessageVo();
            messageVo.setClientId(String.valueOf(i));

            List<String> list2 = new ArrayList<>();
            list2.add("1");
            list2.add("2");
            testJson.setParentId(list1);
            testJson.setAncestors(list2);
            list.add(testJson);
        }
        List<List<TestJson>> listList = new ArrayList<>();
        Stream.iterate(0, n -> n+1).limit((list.size() + 999)/999).forEach(t->{
            listList.add(list.stream().skip(t*999).limit(999).collect(Collectors.toList()));
        });
        listList.stream().forEach(i->{
            testJsonService.saveBatch(i);
        });
        return true;
    }

    @GetMapping("/list")
    public List<TestJson> list () {
        return testJsonService.list();
    }

    @GetMapping("/select")
    public List<TestJson> select (String id) {
        return testJsonService.list(new LambdaQueryWrapper<TestJson>().apply("json_contains(parent_id, json_object('"+ id +"', '1'))"));
    }
}
